package com.bjsxt.userscenter.springmvc.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.users.pojo.AAdmins;

/**
 * 适配器模式拦截器
 * true：放行  false:拦截
 * @author zhanghuan
 *
 */
@Component("authIntercepter")
public class AuthIntercepter extends HandlerInterceptorAdapter
{
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		String info ="";
		/*如何验证是否登录呢*/
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		if(admins == null)
		{
			/**
			/* 登陆失败,客户端跳转到登陆页面
			 * 跳转到Controller中的方法
			 *  */
			/* 非法访问,请先登陆 */
			info = ConstatFinalUtil.INFO_JSON.getString("7");
			session.setAttribute("info", info);
			response.sendRedirect(request.getContextPath()+"/login.htm");
			return false;
		}else
		{
			/*登录成功*/
			return true;
		}
	}
	
}
